package org.jet.emall.prod.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jet.emall.common.constant.CustomerAddrDefaultValue;
import org.jet.emall.prod.dto.CustomerAddrDTO;
import org.jet.emall.prod.dto.CustomerDetailsDTO;
import org.jet.emall.prod.dto.NewCustomerDTO;
import org.jet.emall.prod.po.Customer;
import org.jet.emall.prod.po.CustomerAddr;
import org.jet.emall.prod.po.CustomerInfo;
import org.jet.emall.prod.po.CustomerLevelInfo;
import org.jet.emall.prod.service.mp.ICustomerAddrService;
import org.jet.emall.prod.service.mp.ICustomerInfoService;
import org.jet.emall.prod.service.mp.ICustomerLevelInfoService;
import org.jet.emall.prod.service.mp.ICustomerService;
import org.jet.emall.prod.transfer.CustomerDtoTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-23 12:09
 */
@Service
public class CustomCustomerServiceImpl implements CustomCustomerService {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICustomerInfoService customerInfoService;
    @Autowired
    private ICustomerLevelInfoService customerLevelInfoService;

    @Autowired
    private ICustomerAddrService customerAddrService;

    @Override
    public CustomerDetailsDTO getCustomer(Long id) {
        Customer customer = customerService.getById(id);
        CustomerInfo customerInfo = customerInfoService.getOne(new QueryWrapper<CustomerInfo>().eq("customer_id", id));
        CustomerLevelInfo levelInfo = customerLevelInfoService.getById(customerInfo.getLevelId());
        return new CustomerDetailsDTO().setId(customer.getId())
                .setUsername(customer.getUsername())
                .setLocked(customer.getLocked())
                .setBalance(customerInfo.getBalance())
                .setBirthDate(customerInfo.getBirthDate())
                .setEmail(customerInfo.getEmail())
                .setIdentityCardNo(customerInfo.getIdentityCardNo())
                .setIdentityCardType(customerInfo.getIdentityCardType())
                .setGender(customerInfo.getGender())
                .setPoint(customerInfo.getPoint())
                .setLevel(levelInfo.getName())
                .setMobilePhone(customerInfo.getMobilePhone());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(NewCustomerDTO dto) {
        Customer customer = new Customer();
        customer.setUsername(dto.getUsername())
                .setPassword(dto.getPassword());
        customerService.save(customer);
        CustomerInfo customerInfo = new CustomerInfo()
                .setCustomerId(customer.getId())
                .setBirthDate(dto.getBirthDate())
                .setGender(dto.getGender())
                .setEmail(dto.getEmail())
                .setIdentityCardNo(dto.getIdentityCardNo())
                .setIdentityCardType(dto.getIdentityCardType())
                .setMobilePhone(dto.getMobilePhone())
                .setRealname(dto.getRealname());
        customerInfoService.save(customerInfo);
    }

    @Override
    public List<CustomerAddrDTO> listAddrs(Long customerId) {
        List<CustomerAddr> list = customerAddrService.list(new QueryWrapper<CustomerAddr>().eq("customer_id", customerId));
        if(list==null||list.isEmpty()){
            return null;
        }
        List<CustomerAddrDTO> dtoList = CustomerDtoTransfer.INSTANCE.toAddrDtoList(list);
        return dtoList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveAddr(CustomerAddrDTO dto) {
        int count = customerAddrService.count(new QueryWrapper<CustomerAddr>().eq("customer_id", dto.getCustomerId()));

        CustomerAddr addr = CustomerDtoTransfer.INSTANCE.toAddrPo(dto);
        if(count==0){
            addr.setIsDefault(CustomerAddrDefaultValue.DEFAULT);
        }
        addr.setId(null);
        customerAddrService.save(addr);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAddr(CustomerAddrDTO dto) {
        CustomerAddr addr =  CustomerDtoTransfer.INSTANCE.toAddrPo(dto);
        customerAddrService.updateById(addr);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDefaultAddr(Long customerId, Long addrId) {
        customerAddrService.update(new UpdateWrapper<CustomerAddr>().eq("customer_id",customerId).set("is_default",CustomerAddrDefaultValue.NOT_DEFAULT));
        customerAddrService.updateById( new CustomerAddr().setId(addrId).setIsDefault(CustomerAddrDefaultValue.DEFAULT) );
        
    }
}
